package stack_queue.leetcode;

/**
 * 设计循环队列
 */
public class MyCircularQueue {
    private int[] data;
    private int front;
    private int tail;
    public MyCircularQueue(int k) {
        data = new int[k + 1];
    }

    public boolean enQueue(int value) {
        if (isFull()) {
            return false;
        }
        data[tail] = value;
        tail = (tail + 1) % data.length;
        return true;
    }

    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        front = (front + 1) % data.length;
        return true;
    }

    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return data[front];
    }

    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        int index = tail == 0 ? data.length - 1 : tail - 1;
        return data[index];
    }

    public boolean isEmpty() {
        return front == tail;
    }

    public boolean isFull() {
        return (tail + 1) % data.length == front;
    }
}
